//
//  FriendViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/27.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "FriendViewController.h"

#import "UIView+SDAutoLayout.h"

#import "ListTableViewCell.h"

@interface FriendViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic , strong)UISegmentedControl *typeControl;

//设置详细视图
@property (nonatomic , strong)UIScrollView * detailView;

@property (nonatomic , strong)NSMutableArray *listArray;

@end

@implementation FriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    //设置列表切换
    self.typeControl = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"好友",@"关注"]];
    self.typeControl.selectedSegmentIndex = 0;
    //设置导航栏头视图
    self.navigationItem.titleView = self.typeControl;
    //设置格式
    self.typeControl.sd_layout
    .heightIs(30)
    .widthIs([UIScreen mainScreen].bounds.size.width*0.5);
    
    //添加事件
    [self.typeControl addTarget:self action:@selector(changeType:) forControlEvents:(UIControlEventValueChanged)];
    
    self.view.backgroundColor = [UIColor greenColor];
    
    //初始化detailView
    
    self.detailView = [[UIScrollView alloc] init];
    self.detailView.contentSize = CGSizeMake(self.view.frame.size.width*3,1);
    NSLog(@"%f",self.view.frame.size.width*3);
    self.detailView.pagingEnabled = YES;
    self.detailView.showsHorizontalScrollIndicator = NO;
    self.detailView.showsVerticalScrollIndicator = NO;
    self.detailView.backgroundColor = [UIColor cyanColor];
    self.detailView.delegate = self;
    [self.view addSubview:self.detailView];
    self.detailView.sd_layout
    .topSpaceToView(self.view,0)
    .leftSpaceToView(self.view,0)
    .widthRatioToView(self.view,1)
    .heightRatioToView(self.view,1);
    
    
    
    //初始化listArray
    for (int i = 0 ; i<3; i++) {

        UITableView *temp = [[UITableView alloc] initWithFrame:self.detailView.frame style:(UITableViewStylePlain)];
        temp.delegate = self;
        temp.dataSource = self;
        [self.detailView addSubview:temp];
        [self.listArray addObject:temp];
        temp.sd_layout
        .topSpaceToView(self.detailView,0)
        .leftSpaceToView(self.detailView,[UIScreen mainScreen].bounds.size.width*i)
        .widthRatioToView(self.detailView,1)
        .heightRatioToView(self.detailView,1);
        
    }
    
    
    
    NSLog(@"%@",self.listArray);
}

//懒加载listArray
-(NSMutableArray *)listArray{
    
    if (_listArray == nil) {
        _listArray = [NSMutableArray new];
    }
    return _listArray;
}

//实现切换事件

-(void)changeType:(UISegmentedControl*)sender{
    
    NSLog(@"%ld",sender.selectedSegmentIndex);
    
    [self.detailView setContentOffset:CGPointMake(sender.selectedSegmentIndex*self.detailView.frame.size.width, 0) animated:YES];
}

//实现监听事件

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    if ([scrollView isEqual:self.detailView]) {
        
        CGPoint offset2=[scrollView contentOffset];
        
        self.typeControl.selectedSegmentIndex = offset2.x/[UIScreen mainScreen].bounds.size.width;
        
    }
    
    
    
}
#pragma mark -----实现tableview-----
//实现

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if([tableView isEqual:self.listArray[0]]){
        
        return 10;
        
    }else if([tableView isEqual:self.listArray[1]]){
        
        return 15;
        
    }else{
        
        return 20;
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    
//    if(cell == nil)
//    {
//        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
//    }
//
    //    //根据不同的内容设置cell
    //    if([tableView isEqual:self.listArray[0]]){
    //
    //        cell.textLabel.text = @"消息";
    //
    //
    //    }else if([tableView isEqual:self.listArray[1]]){
    //
    //
    //        cell.textLabel.text = @"好友";
    //
    //    }else{
    //
    //        cell.textLabel.text = @"关注";
    //        
    //    }
    
    
    ListTableViewCell *cell = [ListTableViewCell cellForTableView:tableView];
    
    //根据不同的内容设置cell
    if([tableView isEqual:self.listArray[0]]){
    
        [cell setMessageCellInfo];
    
    
    }else if([tableView isEqual:self.listArray[1]]){
    
        [cell setContactCellInfo];
        
    }else{
        
        [cell setContactCellInfo];
            
    }

    return cell;
    
}

//设置cell高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

//设置cell点击事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
